package cn.lll.vuehouse.controller;


import cn.lll.vuehouse.FORM.PasswordForm;
import cn.lll.vuehouse.FORM.UserLoginForm;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.UserDto;
import cn.lll.vuehouse.entity.User;
import cn.lll.vuehouse.service.UserService;
import cn.lll.vuehouse.utils.JWTUtil;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;


    @PostMapping("login")
    public ResponseResult login(@RequestBody UserLoginForm userLoginForm, HttpServletRequest httpServletRequest){


        return userService.login(userLoginForm,httpServletRequest);
    }


    // 获取菜单  根据token getMenu
    @GetMapping("getMenu")
    public ResponseResult getMenu(HttpServletRequest request){
        return userService.getMenu(request);

    }

    // 获取用户个人信息
    @GetMapping("userForm")
    public ResponseResult getPersonalInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        //解析token  获取 userId
        Integer userId = (Integer) JWTUtil.getClaim(token).get("userId");
        User user = userService.getById(userId);

        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,user);
        return responseResult;
    }

    @GetMapping("updateUser")
    public ResponseResult updateUser(User user) {
        ResponseResult responseResult = new ResponseResult();
        userService.updateById(user);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    @GetMapping("updatePassword")
    public ResponseResult updatePassword(PasswordForm passwordForm,HttpServletRequest request){
        ResponseResult responseResult = new ResponseResult();

        String token = request.getHeader("token");
        Integer userId = (Integer) JWTUtil.getClaim(token).get("userId");
        User byId = userService.getById(userId);
        String pwd = byId.getPwd();
        String salt = byId.getSalt();
        String oldPassword = passwordForm.getOldPassword();
        String md5Hash = new Md5Hash(oldPassword,salt,1024).toHex();
        // 验证旧密码是否正确
        if (!pwd.equals(md5Hash)){
            responseResult.fail(ResponseStatus.INCORRECT_CREDENTIALS);
            return responseResult;
        }

        // 修改新密码
        String newPassword = passwordForm.getNewPassword();
        String md5Hash1 = new Md5Hash(newPassword, salt, 1024).toHex();
        byId.setPwd(md5Hash1);
        userService.updateById(byId);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    @PostMapping("getCurrentPageUserInfo")
    public ResponseResult getCurrentPageUserInfo(@RequestBody UserDto userDto) throws InvocationTargetException, IllegalAccessException {
        return userService.getCurrentPageUserInfo(userDto);
    }

    @GetMapping("getFullUserInfo")
    public ResponseResult getFullUserInfo(Integer id) throws InvocationTargetException, IllegalAccessException {
        return userService.getFullUserInfo(id);
    }

    // 改变用户状态
    @PutMapping("changeUserAvailable")
    public ResponseResult changeUserAvailable(@RequestBody User user){
        return userService.changeUserAvailable(user);
    }

    // 批量删除和单删共用接口
    @DeleteMapping("batchDeleteUser")
    public ResponseResult batchDeleteUser(@RequestBody List<Integer> ids){
        return userService.batchDeleteUser(ids);
    }

    // 新增和更新通用接口
    @PostMapping("addAndUpdateUser")
    public ResponseResult addAndUpdateUser(@RequestBody User user){
        return userService.addAndUpdateUser(user);
    }
}
